home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Apple Event Objects and You / Apple Event Objects (code) / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  3.6 KB  |  167 lines  |  [TEXT/KAHL]

  1. /* File: main.c */
  2.  
  3. #define        __Main__
  4. #include "ScriptScrap.h"
  5. #include "Prototypes.h"
  6.  
  7. void Init_all(void)
  8. {
  9.     Handle         a_menu_bar;
  10.     EventRecord    one_null_event;
  11.     short        count;
  12.     ProcessSerialNumber selfPSN = { 0, kCurrentProcess };
  13.     
  14.     InitGraf(&thePort);                      /*    init Quickdraw and global variables*/
  15.     InitFonts();                           /*    initialize Font manager        */
  16.     InitWindows();                           /* init Window manager and setup WMgr GrafPort*/
  17.     InitMenus();
  18.     TEInit();                              /* inititalize TextEdit                */
  19.     InitDialogs(NULL);                    /* initialize Dialog manager        */
  20.  
  21.     FlushEvents(everyEvent, 0);            /*clear the Event queue of all events */
  22.  
  23.     a_menu_bar = GetNewMBar(1001);
  24.     SetMenuBar(a_menu_bar);    
  25.     AddResMenu(GetMHandle(kAppleMenu), 'DRVR');
  26.     DrawMenuBar();
  27.     
  28.     gDone = FALSE;                          /* initialize flag to control Main Event Loop    */
  29.     InstallAppleEventHandlers();
  30.     
  31.     /* Set up the address descriptor used for sending Apple events to ourself */
  32.     (void)AECreateDesc(typeProcessSerialNumber, (Ptr)&selfPSN, sizeof(selfPSN), &gSelfAddressDesc);
  33.  
  34.     /* Make sure we're the frontmost application when launched */
  35.     for (count = 1; count <= 5; count++)
  36.         WaitNextEvent(0, &one_null_event, 0, 0);
  37.         
  38.     gInFront = TRUE;
  39.     InitCursor();
  40. }
  41.  
  42.  
  43. void DoKey (EventRecord *theEvent)
  44. {
  45.     char        keyPressed = (theEvent->message & charCodeMask);
  46.  
  47.     if (theEvent->modifiers & cmdKey) {
  48.             AdjustMenus();
  49.             Dispatch(MenuKey(keyPressed));        /*Command key down*/
  50.     }
  51. } /* DoKey */
  52.  
  53.  
  54.  
  55.  
  56. void DoMouseDown (EventRecord *theEvent)
  57. {
  58.     Point        globalPt = theEvent->where;
  59.     short        windowPart;
  60.     WindowPtr    whichWindow;
  61.     Rect        dragRect;
  62.  
  63.     windowPart = FindWindow(globalPt, &whichWindow);
  64.     switch (windowPart) {
  65.         case inMenuBar: 
  66.             AdjustMenus();
  67.             Dispatch(MenuSelect(globalPt));
  68.             break;
  69.  
  70.         case inSysWindow: 
  71.             SystemClick(theEvent, whichWindow);
  72.             break;
  73.  
  74.  
  75.         case inGoAway: 
  76.             if (TrackGoAway(whichWindow, theEvent->where))
  77.                 CloseAWindow(whichWindow);
  78.             break;
  79.  
  80.         case inDrag: 
  81.             dragRect = screenBits.bounds;
  82.             dragRect.top = 40;
  83.             DragWindow(whichWindow, theEvent->where, &dragRect);
  84.             break;
  85.  
  86.         case inContent: 
  87.                 DoContentClick(whichWindow, globalPt);
  88.             break;
  89.  
  90.     }
  91. } /* DoMouseDown */
  92.  
  93.  
  94. void MainLoop(void)
  95. {
  96.     EventRecord    theEvent;
  97.     DialogPtr    whichDialog;
  98.     short        itemHit;
  99.  
  100.     while (!gDone) {
  101.         WaitNextEvent(everyEvent, &theEvent, 120L, NULL);        
  102.         switch (theEvent.what) {
  103.             case nullEvent:
  104.                 InitCursor();
  105.             break;
  106.  
  107.             case mouseDown: 
  108.                 DoMouseDown(&theEvent);
  109.             break;
  110.  
  111.             case keyDown:
  112.             case autoKey: 
  113.                 DoKey(&theEvent);
  114.             break;
  115.  
  116.             case activateEvt: 
  117.                 DoActivate((WindowPtr)theEvent.message, theEvent.modifiers & 1);
  118.             break;
  119.  
  120.             case updateEvt:
  121.                 DoUpdate((WindowPtr)theEvent.message);
  122.             break;
  123.             
  124.             case osEvt:
  125.                 /* Handle "Suspend" and "Resume" events */
  126.                 if ((theEvent.message >> 24) == suspendResumeMessage) {
  127.                     gInFront = theEvent.modifiers & 1;
  128.                     if (FrontWindow())
  129.                         DoActivate(FrontWindow(), gInFront);
  130.                 }
  131.             break;
  132.             
  133.             case kHighLevelEvent:
  134.                 (void)AEProcessAppleEvent(&theEvent);
  135.         }
  136.     }
  137. } /* MainEventLoop */
  138.  
  139.  
  140. void Shutdown()
  141. {
  142.     /* We've been asked to quit, so close all open windows */
  143.     WindowPeek    currWindow;
  144.     WindowPeek    nextWindow;
  145.     
  146.     currWindow = (WindowPeek)FrontWindow();
  147.     while (currWindow) {
  148.         nextWindow = currWindow->nextWindow;
  149.         CloseAWindow((WindowPtr)currWindow);
  150.         currWindow = nextWindow;
  151.     }
  152. }
  153.  
  154.  
  155. void main(void)
  156. {
  157.     MaxApplZone();
  158.     MoreMasters();               /* create more master pointers    */
  159.     MoreMasters();               /* create more master pointers    */
  160.     MoreMasters();               /* create more master pointers    */
  161.     MoreMasters();               /* create more master pointers    */
  162.     
  163.     Init_all();                        /* call initialization routine    */
  164.     AdjustMenus();
  165.     MainLoop();
  166.     Shutdown();
  167. }